import requests
from lxml import etree
import os
if __name__=="__main__":
    url="http://pic.netbian.com/4kmeinv"
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'
    }
    response=requests.get(url=url,headers=headers).text
    # response.encode('utf-8')

    tree = etree.HTML(response)
    li_list=tree.xpath('//div[@class="slist"]/ul/li')

    if not os.path.exists('./productions'):
        os.mkdir('./productions')
    for li in li_list:
        img_src = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0]
        img_name = li.xpath('./a/img/@alt')[0]+'.jpg'
        # img_name = img_name.encode('ios-8859-1').decode('gbk')
        # print(img_name,img_src)

        # 用上诉得来的图片url逐个请求并下载
        img_date=requests.get(url=img_src,headers=headers).content
        img_path = 'productions/'+img_name
        with open(img_path,'wb') as fp:
            fp.write(img_date)
            print(img_name,'下载成功')